package com.blog.boot.api.common;

import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * 消息返回组件
 * */
public class RestMsg<T> extends MapFactory {
    //返回状态代码
    public static final String RESTSRE_CODE = "code";
    //消息
    public static final String RESTSRE_MSG = "msg";
    //封装数据
    public static final String RESTSRE_DATA = "data";

    RestMsg(){}

    public class RestModelAndView extends ModelAndView{
        RestModelAndView(String viewPath){
            super(viewPath);
        }
    }

    public RestMsg success(String msg) {
        this.put(RESTSRE_CODE,"200");
        this.put(RESTSRE_MSG,msg);
        return this;
    }
    public RestMsg setMsg(String msg) {
        this.put(RESTSRE_MSG,msg);
        return this;
    }
    public RestMsg error(String error){
        this.put(RESTSRE_CODE,"999");
        this.put(RESTSRE_MSG,error);
        return this;
    }

    public RestMsg setData(T data) {
        if (null != data ) {
            this.put(RESTSRE_DATA,data);
        } else {
            this.put(RESTSRE_DATA,new ArrayList<>());
        }
        return this;
    }

    public RestMsg setCode(String code){
        this.put(RESTSRE_CODE,code);
        return this;
    }

}
